package com.bilin.coin.network.callback;

import com.bilin.coin.utils.xml.XML;

import org.json.JSONObject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


/**
 *
 */

public abstract class CommonCallBackXml implements Observer<String> {


    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(String s) {
        try {
            if (!s.equals("") && s != null) {
                JSONObject jsonObject = XML.toJSONObject(s);
                JSONObject rep = jsonObject.getJSONObject("MEBS_MOBILE").getJSONObject("REP");
                JSONObject result = rep.getJSONObject("RESULT");
                JSONObject resultList = null;
                if (s.contains("RESULTLIST")) {
                    resultList = rep.getJSONObject("RESULTLIST");
                }
                if (result != null || resultList != null) {
                    if (resultList != null) {
                        onSuccessResultList(resultList.toString());
                    } else {
                        onSuccessResult(result.toString());
                    }
                } else {
                    onError("没有数据返回");
                }

            } else {
                onCallBackError(new Throwable("-400", new Throwable("返回数据为空")));
            }
        } catch (Exception e) {
            onCallBackError(new Throwable(e.toString(), new Throwable(e)));
            e.printStackTrace();
        }
    }

    protected void onSuccessResult(String result) {

    }

    protected void onSuccessResultList(String resultList) {

    }

    protected abstract void onError(String errorMsg);


    protected abstract void onCallBackError(Throwable e);

    @Override
    public void onError(Throwable e) {
        onCallBackError(e);
    }

    @Override
    public void onComplete() {

    }
}
